﻿using System;
using System.Data;
using System.Drawing;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Baimasoft.Manage.SqlServerDAL;

public partial class SysManage_DataRole_List : System.Web.UI.Page
{
    DbRoleManage MyDbRM = new DbRoleManage();
    public int type = 0;
    public string m_sRole_Code = "", m_sRole_Name = "";
    public int AllCount = 0, AllPageCount = 0, NowPageCount = 0, PageSize = 0;
    DataTable MyTB;

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_User_LogonID"] == null)
        {
            Response.Redirect("../ReLogon.aspx");
        }
        if (!Page.IsPostBack)
        {
            GridView1.Attributes.Add("BorderColor", "LightSkyBlue"); 
            if (ViewState["OpType"] == null)
            {
                ViewState["OpType"] = "0";
            }

            if (ViewState["SortExpression"] == null || ViewState["SortExpression"].ToString().CompareTo("") == 0)
            {
                ViewState["SortExpression"] = "Role_Code";
            }

            if (ViewState["SortDirection"] == null || ViewState["SortDirection"].ToString().CompareTo("") == 0)
            {
                ViewState["SortDirection"] = " ";
            }
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];

            Bind(sSqlSort);
        }
    }

    private void Bind(string sqlsort)
    {
        if (ViewState["OpType"].ToString() == "0")
        {
            MyTB = MyDbRM.SearchDbRoleInfo("1", "", "", sqlsort);
        }
        else if (ViewState["OpType"].ToString() == "1")  //查询状态
        {
            MyTB = MyDbRM.SearchDbRoleInfo("", m_sRole_Code.Trim(), m_sRole_Name.Trim(), sqlsort);
        }
        else if (ViewState["OpType"].ToString() == "2") // 显示删除
        {
            MyTB = MyDbRM.SearchDbRoleInfo("0,1", "", "", sqlsort);
        }
        GridView1.DataSource = MyTB;
        GridView1.DataBind();
        AllPageCount = GridView1.PageCount;
        NowPageCount = GridView1.PageIndex + 1;
        AllCount = MyTB.Rows.Count;
    }
    protected void SearchCheckInInfo_ServerClick(object sender, EventArgs e)
    {
        ViewState["OpType"] = "1";
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        m_sRole_Code = Role_Code.Text.Trim();
        m_sRole_Name = Role_Name.Text.Trim();
        Bind(sSqlSort);
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            if (e.Row.RowIndex % 2 != 0)
            {
                e.Row.Attributes.Add("onclick", "GetRowInfo('" + e.Row.Cells[0].Text.ToString().Trim() + "~" + e.Row.Cells[1].Text.ToString().Trim() + "~" + e.Row.Cells[2].Text.ToString().Trim() + "~" + e.Row.Cells[3].Text.ToString().Trim() + "~" + e.Row.Cells[4].Text.ToString().Trim() + "~" + e.Row.Cells[5].Text.ToString().Trim() + "~" + e.Row.Cells[6].Text.ToString().Trim() + "~" + e.Row.Cells[7].Text.ToString().Trim() + "~" + e.Row.Cells[8].Text.ToString().Trim() + "~" + e.Row.Cells[9].Text.ToString().Trim() + "~" + e.Row.Cells[10].Text.ToString().Trim() + "~" + e.Row.Cells[11].Text.ToString().Trim() + "~" + e.Row.Cells[12].Text.ToString().Trim() + "~" + MyDbRM.GetFORoleName(e.Row.Cells[3].Text.ToString().Trim()) + "'),tog(this,'#EFEFF8')");
            }
            else
            {
                e.Row.Attributes.Add("onclick", "GetRowInfo('" + e.Row.Cells[0].Text.ToString().Trim() + "~" + e.Row.Cells[1].Text.ToString().Trim() + "~" + e.Row.Cells[2].Text.ToString().Trim() + "~" + e.Row.Cells[3].Text.ToString().Trim() + "~" + e.Row.Cells[4].Text.ToString().Trim() + "~" + e.Row.Cells[5].Text.ToString().Trim() + "~" + e.Row.Cells[6].Text.ToString().Trim() + "~" + e.Row.Cells[7].Text.ToString().Trim() + "~" + e.Row.Cells[8].Text.ToString().Trim() + "~" + e.Row.Cells[9].Text.ToString().Trim() + "~" + e.Row.Cells[10].Text.ToString().Trim() + "~" + e.Row.Cells[11].Text.ToString().Trim() + "~" + e.Row.Cells[12].Text.ToString().Trim() + "~" + MyDbRM.GetFORoleName(e.Row.Cells[3].Text.ToString().Trim()) + "'),tog(this,'#F9F9FB')");
            }
            e.Row.Cells[3].Text = MyDbRM.GetFORoleName(e.Row.Cells[3].Text.ToString().Trim());
            if (e.Row.Cells[5].Text.Trim() == "1")
                e.Row.Cells[5].Text = "是";
            if (e.Row.Cells[5].Text.Trim() == "0")
            {
                e.Row.ForeColor = Color.Red;
                e.Row.Cells[5].Text = "否";
            }
        }
    }
    protected void BtnExport_Click(object sender, EventArgs e)
    {

    }
    protected void BtnDel_Click(object sender, EventArgs e)
    {
        string sCheckInCode = HCheckInCode.Value.ToString().Trim();
        int iback = MyDbRM .IFDelDataRoleInfo (sCheckInCode, "0");
        if (iback == 1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除成功!');</script>");
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            Bind(sSqlSort);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除失败!');</script>");
        }
    }
    protected void BtnDispDel_Click(object sender, EventArgs e)
    {
        ViewState["OpType"] = "2";
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);
    }
    protected void BtnHideDel_Click(object sender, EventArgs e)
    {
        ViewState["OpType"] = "0";
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);
    }
    protected void BtnRefresh_Click(object sender, EventArgs e)
    {
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);
    }
    protected void BtnResume_Click(object sender, EventArgs e)
    {
        string sCheckInCode = HCheckInCode.Value.ToString().Trim();
        int iback = MyDbRM.IFDelDataRoleInfo(sCheckInCode, "1");
        if (iback == 1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('恢复成功!');</script>");
            ViewState["OpType"] = "2";
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            Bind(sSqlSort);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('恢复失败!');</script>");
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortDirection"].ToString() == "desc")
        {
            ViewState["SortDirection"] = " ";
        }
        else
        {
            ViewState["SortDirection"] = "desc";
        }

        ViewState["SortExpression"] = e.SortExpression;
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);
    }
}
